@echo off
setlocal ENABLEDELAYEDEXPANSION
echo.
:setgxdev
set USERQ=
set GXDEV=
for %%f in (gxc.exe) do (
	if not "%%~$PATH:f"=="" (
		goto start
	)
)

echo **** Choose GX developer folder ****
echo 1:  C:\Program Files (x86)\Geosoft\GX Developer\gx\bin
echo 2:  C:\Program Files\Geosoft\GX Developer\gx\bin
echo ------------------------------------
echo Choose number from list above corresponding to your installation folder or enter your own directory
set /p USERQ=": "
echo ************************************
echo.
if "%USERQ%"=="" (
	echo *** Make a choice ***
	goto setgxdev
)

set GXDEV=%USERQ%
if %USERQ% EQU 1 set GXDEV=C:\Program Files (x86)\Geosoft\GX Developer\gx\bin\
if %USERQ% EQU 2 set GXDEV=C:\Program Files\Geosoft\GX Developer\gx\bin\

if exist "%GXDEV%" goto done_dev_chk

echo %GXDEV% does not exist
echo.
goto setgxdev

:done_dev_chk
set path=%path%;%GXDEV%;

:start
set UPRJ=
echo ---------------------------------------------
echo directory list of gxc files in current directory:
echo ---------------------------------------------
dir /b /s *.gxc > gxclst.lst
set ITR=1
FOR /F "eol=/ tokens=1-3 delims=	" %%j IN (gxclst.lst) DO (
	echo !ITR!:	%%j
	set /A ITR=!ITR!+1
)
echo ---------------------------------------------
echo Choose which file you want to compile (enter q to quit)
set /p UPRJ=": "

IF /I "%UPRJ%"=="q" goto end

IF /I "%UPRJ%"=="" (
	echo ******
	echo please make a choice
	echo ******
	goto start
)

set /A ITR=1
FOR /F "eol=/ tokens=1-3 delims=	" %%j IN (gxclst.lst) DO (
	IF /I %UPRJ% EQU !ITR! (
		set FILE=%%~nj
		set FDIR=%%~dpj
		)
	set /A ITR=!ITR!+1
)
if exist gxclst.lst del gxclst.lst
echo ************************************
echo.

pushd %FDIR%
grc %FILE%
gxc %FILE%
popd

:setoutput
set USERQ=
echo ************************************
echo.

echo **** Choose Output folder ****
echo 1:  C:\Program Files (x86)\Geosoft\Oasis montaj\gx\
echo 2:  C:\Program Files\Geosoft\Oasis montaj\gx\
echo 3:  C:\Program Files (x86)\Geosoft\Oasis montaj Viewer\gx\
echo 4:  C:\Program Files (x86)\Geosoft\Oasis montaj Viewer\gx\
echo ------------------------------------
echo copy gx to local gx directory? Choose number from list above enter your own directory 
echo press n to skip, or q to quit
set /p USERQ=": "
echo.
if "%USERQ%"=="" (
	echo *** Make a choice ***
	goto setoutput
)
echo ************************************
echo.

if "%USERQ%"=="n" goto start
if "%USERQ%"=="q" goto end

set GXDIR=%USERQ%
if %USERQ% EQU 1 set GXDIR=C:\Program Files (x86)\Geosoft\Oasis montaj\gx\
if %USERQ% EQU 2 set GXDIR=C:\Program Files\Geosoft\Oasis montaj\gx\
if %USERQ% EQU 3 set GXDIR=C:\Program Files (x86)\Geosoft\Oasis montaj Viewer\gx\
if %USERQ% EQU 4 set GXDIR=C:\Program Files (x86)\Geosoft\Oasis montaj Viewer\gx\

if exist "%GXDIR%" goto done_out_chk

echo %GXDIR% does not exist
echo.
goto setoutput


:done_out_chk
pushd "%FDIR%"
call copy %FILE%.gx "%GXDIR%"
popd
echo ************************************
echo.

goto start

:end
if exist gxclst.lst del gxclst.lst
